home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_333 / multiplot / source / plt2dr_src / p2d.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  198 lines

  1. #include <exec/types.h>
  2. #include <stdio.h>
  3. #include <exec/memory.h>
  4. #include "libraries/dos.h"
  5. #include "exec/exec.h"
  6. #include "intuition/intuitionbase.h"
  7. #include "devices/keymap.h"
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <stdlib.h>
  11.  
  12. #include "drawicon.h"
  13. struct IntuitionBase *IntuitionBase;
  14. long IconBase;
  15.  
  16. #define STOP 0
  17. #define GO 1
  18. int StopFlag = GO;
  19.  
  20. #define OPEN 1
  21. #define CLOSED 0
  22. int PartFlag = CLOSED;
  23.  
  24. char line[100];
  25.  
  26. struct coord {
  27.     float x;
  28.     float y;
  29.     struct coord *next_coord;
  30. };
  31.  
  32. struct part {
  33.     float minx;
  34.     float maxx;
  35.     float miny;
  36.     float maxy;
  37.     int colour;
  38.     struct coord *first_coord;
  39.     struct part *next_part;
  40. };
  41.  
  42. char plot_name[100];
  43. char draw_name[100];
  44.  
  45.  
  46.  
  47. void main(argc,argv)
  48. int argc;
  49. char *argv[];
  50. {
  51. float x,y;
  52. float MinX, MinY, MaxX, MaxY;
  53. FILE *fp1, *fp2;
  54. struct part *Part, *FirstPart;
  55. struct coord *Coord;
  56. int FLAG,i,PartNumber;
  57.  
  58.   /*** PARSE ARGS ***/
  59. MinX=MinY=MaxX=MaxY=0;
  60. PartNumber=0;
  61.  
  62.   if ((argv[1][0] == '?')||(argc!=3))
  63.      {
  64.         printf("usage: Plot2Draw infile outfile\n");
  65.         exit(0);
  66.      }
  67.    else
  68.      {
  69.        strcpy(plot_name,argv[1]);
  70.        strcpy(draw_name,argv[2]);
  71.      }
  72.  
  73.    /*** OPEN LIBRARIES ***/
  74.  
  75.    if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0))) {
  76.       printf("Can't open intuition library...\n");
  77.       exit(0);
  78.    }
  79.    if (!(IconBase = OpenLibrary("icon.library", 0))) {
  80.       printf("Can't open icon library...\n");
  81.       CloseLibrary(IntuitionBase);
  82.       exit(0);
  83.    }
  84.  
  85.    /*** GET DATA FROM FILE ***/
  86.  
  87.    fp1 = NULL;
  88.    if (*plot_name) fp1 = fopen(plot_name,"r");
  89.    if (fp1==NULL)
  90.       {
  91.          CloseLibrary(IntuitionBase);
  92.          CloseLibrary(IconBase);
  93.          printf("Input file does not exist.\n");
  94.          exit(0);
  95.       }
  96.    fp2 = NULL;
  97.    if (*draw_name) fp2 = fopen(draw_name,"w");
  98.    if (fp2==NULL)
  99.       {
  100.          fclose(fp1);
  101.          CloseLibrary(IntuitionBase);
  102.          CloseLibrary(IconBase);
  103.          printf("Unable to open Output File.\n");
  104.          exit(0);
  105.       }
  106.  
  107.   Part=(struct part *)malloc(sizeof(struct part));
  108.   FirstPart=Part;
  109.   Coord=(struct coord *)malloc(sizeof(struct coord));
  110.  
  111. while (StopFlag==GO) {
  112.   if (fgets(line,100,fp1)==NULL)
  113.     {
  114.          Part->next_part=NULL;
  115.          StopFlag=STOP;
  116.     }
  117.   else
  118.     {
  119.       FLAG = sscanf(line,"%f %f",&x,&y);
  120.       if ((line[0]=='*')&&(PartFlag==OPEN))
  121.          {
  122.             if (line[1]=='C')                   /* Get the colour */
  123.                {
  124.                   PartFlag=CLOSED;
  125.                   Part->colour=atoi(&line[3]);
  126.                   MinX=min(MinX,Part->minx);
  127.                   MaxX=max(MaxX,Part->maxx);
  128.                   MinY=min(MinY,Part->miny);
  129.                   MaxY=max(MaxY,Part->maxy);
  130.                   Coord->next_coord=NULL;
  131.                   Part->next_part=(struct part *)malloc(sizeof(struct part));
  132.                   Part=Part->next_part;
  133.                }
  134.          }
  135.       else if (FLAG>1)       /* Is it numbers? */
  136.         {
  137.             if (PartFlag==CLOSED)         /* A new part? */
  138.                {
  139.                    PartFlag=OPEN;
  140.                    PartNumber++;
  141.                    Part->first_coord=(struct coord *)malloc(sizeof(struct coord));
  142.                    Coord=Part->first_coord;
  143.                    Coord->x =x;
  144.                    Coord->y =y;
  145.                    Part->minx=x;
  146.                    Part->maxx=x;
  147.                    Part->miny=y;
  148.                    Part->maxy=y;
  149.                }
  150.             else                           /* Continuing a part */
  151.               {
  152.                    Coord->next_coord=(struct coord *)malloc(sizeof(struct coord));
  153.                    Coord=Coord->next_coord;
  154.                    Coord->x =x;
  155.                    Coord->y =y;
  156.                    Part->minx=min(Part->minx,x);
  157.                    Part->maxx=max(Part->maxx,x);
  158.                    Part->miny=min(Part->miny,y);
  159.                    Part->maxy=max(Part->maxy,y);
  160.               }
  161.          }
  162.       else if (FLAG==EOF);   /* blank line reached */
  163.     }
  164.  }
  165.  
  166.   fprintf(fp2,"81086 %f %f %f %f 0 1.00000 \"%s\"\n-1\n",MinX,MinY,MaxX,MaxY,draw_name);
  167.   Part=FirstPart;
  168.   for (i=0;i<PartNumber;i++)
  169.     {
  170.        if (Part->first_coord!=NULL)
  171.          {
  172.             fprintf(fp2,"1 52 %f %f %f %f %d 0 0 0 0 \n",Part->minx,Part->miny,Part->maxx,Part->maxy,Part->colour);
  173.             Coord=Part->first_coord;
  174.  
  175.             while (Coord!=NULL)
  176.               {
  177.                  fprintf(fp2,"       1 %f %f\n",Coord->x,Coord->y);
  178.                  Coord=Coord->next_coord;
  179.               }
  180.             fprintf(fp2,"       0\n");
  181.          }
  182.       Part=Part->next_part;
  183.     }
  184.   fprintf(fp2,"-1\n");
  185.  
  186.   fclose(fp1);
  187.   fclose(fp2);
  188.  
  189.   PutDiskObject(draw_name,&IconDiskObject);
  190.  
  191.   CloseLibrary(IntuitionBase);
  192.   CloseLibrary(IconBase);
  193.  
  194.   /* Note: I'm relying on the Lattice compiler to clean up allocated
  195.      memory. Not nice, but saves on file size!    */
  196. }
  197.  
  198.